package com.suning.sawp.service.impl.task;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.service.task.dto.ABSaleCompletion;
import com.suning.sawp.service.task.dto.BIABSaleCompletion;
import com.suning.sawp.service.task.dto.OrgABSaleCompletion;

/**
 * 总部/大区任务统计
 * 
 * @author 13071602
 *
 */
@Service
public class BITaskService {
    @Autowired
    DalClient dalClient;

    /**
     * 查询所有大区
     * 
     * @param taskNo 总部任务号
     */
    public BIABSaleCompletion queryRegions(Integer taskNo) {
        BIABSaleCompletion saleCompletion = new BIABSaleCompletion();
        BeanUtils.copyProperties(sumBIDis(taskNo), saleCompletion);
        listBIRegion(saleCompletion, taskNo);
        return saleCompletion;
    }

    /**
     * 查询大区下所有门店
     * 
     * @param taskNo 大区任务号
     */
    public BIABSaleCompletion queryStores(Integer taskNo) {
        BIABSaleCompletion saleCompletion = new BIABSaleCompletion();
        BeanUtils.copyProperties(sumBIStore(taskNo), saleCompletion);
        listBIStore(saleCompletion, taskNo);
        return saleCompletion;
    }

    /**
     * 查询分大区销售统计
     * 
     * @param taskNo 总部任务编号
     * 
     * @see sqlMap_RegionTaskService.xml
     */
    private List<OrgABSaleCompletion> listBIRegion(BIABSaleCompletion saleCompletion, Integer taskNo) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("taskNo", taskNo);
        List<OrgABSaleCompletion> saleCompletions = dalClient
                .queryForList("RegionTaskService.sumByTaskAndCommTypeOfDis", paramMap, OrgABSaleCompletion.class);
        for (OrgABSaleCompletion row : saleCompletions) {
            stat(row, saleCompletion);
        }
        saleCompletion.setOrgABSaleCompletion(saleCompletions);
        return saleCompletions;
    }

    /**
     * 查询分大区销售统计
     * 
     * @param taskNo 大区任务编号
     * 
     * @see sqlMap_RegionTaskService.xml
     */
    private List<OrgABSaleCompletion> listBIStore(BIABSaleCompletion saleCompletion, Integer taskNo) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("taskNo", taskNo);
        List<OrgABSaleCompletion> saleCompletions = dalClient
                .queryForList("RegionTaskService.sumByTaskAndCommTypeOfStore", paramMap, OrgABSaleCompletion.class);
        for (OrgABSaleCompletion row : saleCompletions) {
            stat(row, saleCompletion);
        }
        saleCompletion.setOrgABSaleCompletion(saleCompletions);
        return saleCompletions;
    }

    /**
     * @see sqlMap_RegionTaskService.xml
     * @param taskNo 总部任务号
     * @return
     */
    private ABSaleCompletion sumBIDis(Integer taskNo) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("taskNo", taskNo);
        ABSaleCompletion saleCompletion = dalClient.queryForObject("RegionTaskService.sumByTaskAndCommTypeOfDisAll",
                paramMap, ABSaleCompletion.class);
        completion(saleCompletion);
        return saleCompletion;
    }

    /**
     * 
     * @param taskNo 大区任务编号
     * @return
     */
    private ABSaleCompletion sumBIStore(Integer taskNo) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("taskNo", taskNo);
        ABSaleCompletion saleCompletion = dalClient.queryForObject("RegionTaskService.sumByTaskAndCommTypeOfStoreAll",
                paramMap, ABSaleCompletion.class);
        completion(saleCompletion);
        return saleCompletion;
    }

    /**
     * 计算完成率/占比/销售额
     */
    private void stat(OrgABSaleCompletion row, BIABSaleCompletion saleCompletion) {
        completion(row);
        rate(row, saleCompletion);
    }

    /**
     * 占比
     * 
     * @param row
     * @param saleCompletion
     */
    private void rate(OrgABSaleCompletion row, BIABSaleCompletion saleCompletion) {
        if (null != row.getSaleAmount()) {
            row.setRate(row.getSaleAmount().divide(saleCompletion.getSaleAmount(), 4, RoundingMode.HALF_UP));
        }
    }

    /**
     * 完成率
     * 
     * @param row
     */
    private void completion(ABSaleCompletion row) {
        row.setCompletion(new BigDecimal(0.6
                * (null != row.getSaleVolumeOfA() && row.getDestNumOfA() > 0
                        ? row.getSaleVolumeOfA() * 1.0 / row.getDestNumOfA() : 0)
                + 0.4 * (null != row.getSaleVolumeOfB() && row.getDestNumOfB() > 0
                        ? row.getSaleVolumeOfB() * 1.0 / row.getDestNumOfB() : 0)).setScale(2, RoundingMode.CEILING));
    }

}
